package com.example.sneakerappforme1105;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.app.FragmentManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.example.sneakerappforme1105.ViewModel.CartViewModel;
import com.example.sneakerappforme1105.adapter.OrderPagerAdapter;
import com.example.sneakerappforme1105.adapter.SalePagerAdapter;
import com.example.sneakerappforme1105.adapter.SneakerPagerAdapter;
import com.example.sneakerappforme1105.bean.CartItem;
import com.example.sneakerappforme1105.bean.Goods;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class SaleActivity extends AppCompatActivity implements DataProvider{
    private ViewPager vp_content;
    private RadioGroup rg_tabbar;
    private Long mUserId;
    private Long mCartId;
    private Fragment fg_home;
    private Fragment fg_order;
    private FragmentManager fManager;
    private String TAG = "SaleActivity";
    private CartViewModel cartViewModel;
    private int fragmentIdx = 0;
    private List<CartItem> mCartItemList = new ArrayList<>();
    private HashMap<Long, Goods> mGoodsMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sale);

        mUserId = getIntent().getLongExtra("userId",0L);
        rg_tabbar = findViewById(R.id.rg_tabbar);
        ((RadioButton)rg_tabbar.getChildAt(fragmentIdx)).setChecked(true);
        vp_content = findViewById(R.id.vp_content); // viewPager
        SalePagerAdapter adapter = new SalePagerAdapter(getSupportFragmentManager());
        vp_content.setAdapter(adapter);

        vp_content.setCurrentItem(fragmentIdx); // 11.16

        // 设置翻页监听
        vp_content.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
            @Override
            public void onPageSelected(int position) {
                rg_tabbar.check(rg_tabbar.getChildAt(position).getId());
            }
        });

        // 设置单选组的选中监听器
        rg_tabbar.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {

                for(int pos=0; pos<rg_tabbar.getChildCount(); pos++){
                    // 获得指定位置的单选按钮
                    RadioButton tab = (RadioButton) rg_tabbar.getChildAt(pos);
                    if(tab.getId() == checkedId){   // 来自参数
                        vp_content.setCurrentItem(pos);
                    }
                }
            }
        });



    }

    @Override
    protected void onResume() { // 来自 GoodsActivity
        super.onResume();
        cartViewModel = MainApplication.getInstance().getCartViewModel();

        cartViewModel.getCartItemList().observe(this, items -> {
            mCartItemList = items != null ? items : new ArrayList<>();

        });

        cartViewModel.getGoodsMap().observe(this, map->{
            mGoodsMap = map !=null ? map: new HashMap<>();
        });

        Log.d(TAG,"Root重新启动"+mCartItemList.size());


    }

    @Override
    public Long getUserId() {
        return mUserId;
    }

    @Override
    public Long getCartId() {
        return mCartId;
    }

    @Override
    public CartViewModel getCartViewModel() {
        return cartViewModel;
    }

    @Override
    public List<CartItem> getCartItemList() {
        return mCartItemList;
    }

    @Override
    public HashMap<Long, Goods> getGoodsMap() {
        return mGoodsMap;
    }


}